<template>
    <transition name="dialog-fade">
        <div class="ff-dialog" v-show="visible" :tag="tag">
            <div class="ff-dialog-body">
                <span class="ff-dialog-close" @click="close" v-if="showClose">
                    <i class="iconfont icon-cha1"></i>
                </span>
                <slot></slot>
            </div>
        </div>
    </transition>
</template>

<script>
    export default {
        props: {
            /*控制弹框显示*/
            visible: {
                type: Boolean,
                default: false
            },
            /*是否放在body中*/
            appendToBody: {
                type: Boolean,
                default: true
            },
            /*是否显示关闭icon*/
            showClose: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {

            }
        },
        computed: {
            tag () {
                return `ff-dialog-tag-${new Date().getTime()}`
            }
        },
        methods: {
            /*关闭弹框*/
            close () {
                this.$emit('update:visible', false);
                this.$emit('close');
            }
        },
        mounted () {
            if (this.appendToBody) {
                document.body.appendChild(this.$el);
            }
            /*this.$nextTick(() => {
                let width = this.$slots.default[0].elm.clientWidth;
            })*/
        }
    }
</script>

<style type="text/less" lang="less">
    .ff-dialog {
        position: fixed;
        z-index: 4;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        overflow: auto;
        margin: 0;
        background-color: rgba(0,0,0,.2);
        display: flex;
        align-items: center;
        justify-content: center;
        .ff-dialog-body {
            display: inline-block;
            background-color: #fff;
            border-radius: 4px;
            position: relative;
            padding: 20px 20px 28px;
            .ff-dialog-close {
                position: absolute;
                right: 6px;
                top: 6px;
                cursor: pointer;
                i {
                    font-size: 25px;
                }
            }
        }
    }
    .dialog-fade-enter-active {
        animation: dialog-fade-in .3s;
    }
    .dialog-fade-leave-active {
        animation: dialog-fade-out .3s;
    }
    @keyframes dialog-fade-in {
        0% {
            transform: translate3d(0, -20px, 0);
            opacity: 0;
        }
        100% {
            transform: translate3d(0, 0, 0);
            opacity: 1;
        }
    }
    @keyframes dialog-fade-out {
        0% {
            transform: translate3d(0, 0, 0);
            opacity: 1;
        }
        100% {
            transform: translate3d(0, -20px, 0);
            opacity: 0;
        }
    }
</style>